
-- NativeUIDemoBasic.lua
-- Author: Mikael Kindborg
-- Date: 2011-02-22
-- Description: Very basic demo of NativeUI in Lua.

-- Widget size constants.
FILL_PARENT = "-1"
WRAP_CONTENT = "-2"

-- Create screen with widgets.

Screen = maWidgetCreate("Screen")
maWidgetScreenShow(Screen)

MainLayout = maWidgetCreate("VerticalLayout")
maWidgetSetProperty(MainLayout, "width", FILL_PARENT)
maWidgetSetProperty(MainLayout, "height", FILL_PARENT)
maWidgetSetProperty(MainLayout, "backgroundColor", "0xFFFFFF")
maWidgetAddChild(Screen, MainLayout)

HeaderLabel = maWidgetCreate("Label")
maWidgetSetProperty(HeaderLabel, "width", FILL_PARENT)
maWidgetSetProperty(HeaderLabel, "height", WRAP_CONTENT)
maWidgetSetProperty(HeaderLabel, "fontSize", "20.0")
maWidgetSetProperty(HeaderLabel, "fontColor", "0x000000")
maWidgetSetProperty(HeaderLabel, "fontSize", "36")
maWidgetSetProperty(HeaderLabel, "text", "Demo of MoSync NativeUI")
maWidgetAddChild(MainLayout, HeaderLabel)

ButtonSayHello = maWidgetCreate("Button")
maWidgetSetProperty(ButtonSayHello, "width", FILL_PARENT)
maWidgetSetProperty(ButtonSayHello, "height", WRAP_CONTENT)
maWidgetSetProperty(ButtonSayHello, "textVerticalAlignment", "center")
maWidgetSetProperty(ButtonSayHello, "textHorizontalAlignment", "center")
maWidgetSetProperty(ButtonSayHello, "text", "Say Hello")
maWidgetSetProperty(ButtonSayHello, "fontColor", "0x000000")
maWidgetSetProperty(ButtonSayHello, "fontSize", "20")
maWidgetAddChild(MainLayout, ButtonSayHello)

ButtonSayHi = maWidgetCreate("Button")
maWidgetSetProperty(ButtonSayHi, "width", FILL_PARENT)
maWidgetSetProperty(ButtonSayHi, "height", WRAP_CONTENT)
maWidgetSetProperty(ButtonSayHi, "textVerticalAlignment", "center")
maWidgetSetProperty(ButtonSayHi, "textHorizontalAlignment", "center")
maWidgetSetProperty(ButtonSayHi, "text", "Say Hi")
maWidgetSetProperty(ButtonSayHi, "fontColor", "0x000000")
maWidgetSetProperty(ButtonSayHi, "fontSize", "20")
maWidgetAddChild(MainLayout, ButtonSayHi)

MessageLabel = maWidgetCreate("Label")
maWidgetSetProperty(MessageLabel, "width", FILL_PARENT)
maWidgetSetProperty(MessageLabel, "height", WRAP_CONTENT)
maWidgetSetProperty(MessageLabel, "fontSize", "20.0")
maWidgetSetProperty(MessageLabel, "fontColor", "0x000000")
maWidgetSetProperty(MessageLabel, "fontSize", "30")
maWidgetSetProperty(MessageLabel, "text", "")
maWidgetAddChild(MainLayout, MessageLabel)

-- Handle widget events.

-- Helper function to get the widget handle.
-- The widget handle is the int at index one.
function WidgetEventGetWidgetHandle(event)
  local widgetEvent = SysEventGetData(event)
  return SysBufferGetInt(widgetEvent, 1)
end

-- Register widget event handling function.
System.onCustomEvent(function(event)
  if EVENT_TYPE_WIDGET == SysEventGetType(event) then
    if ButtonSayHello == WidgetEventGetWidgetHandle(event) then
       maWidgetSetProperty(MessageLabel, "text", "Hello World!")
    elseif ButtonSayHi == WidgetEventGetWidgetHandle(event) then
       maWidgetSetProperty(MessageLabel, "text", "Hi there!")
    end
  end
end)

-- Things to try interactively. Select a line of code (but not
-- comment dashes ("--") and press the "Do selection" button.
-- (Seems there is a bug on Android that makes maWidgetRemoveChild
-- not work.)

-- maWidgetSetProperty(HeaderLabel, "fontSize", "60")
-- maWidgetRemoveChild(MainLayout, HeaderLabel)
-- maWidgetRemoveChild(MainLayout, ButtonSayHello)
-- maWidgetRemoveChild(MainLayout, ButtonSayHi)
-- maExit(0)
